$(document).ready(function(){
	$("#search-button").click(function(){
		if ($("#search_question_field").val() != ''){
			document.location = settings.base_url + lang.lang_key + '/questions/show_questions/' + $("#search_question_field").val()
		} else {
			document.location = settings.base_url + lang.lang_key + '/questions/show_questions';
		}
	});
	
	var nav_label_first = '[label_first]';
	if (lang.label_first != undefined) {
		nav_label_first = lang.label_first;
	}
	
	var nav_label_last = '[label_last]';
	if (lang.label_last != undefined) {
		nav_label_last = lang.label_last;
	}
	var nav_label_prev = '[label_prev]';
	if (lang.label_prev != undefined) {
		nav_label_prev = lang.label_prev;
	}
	var nav_label_next = '[label_next]';
	if (lang.label_next != undefined) {
		nav_label_next = lang.label_next;
	}
	$('#question-list').pajinate({
		items_per_page : 5,
		nav_label_first : nav_label_first,
		nav_label_last : nav_label_last,
		nav_label_prev : nav_label_prev,
		nav_label_next : nav_label_next
	});
	
	var ok = '[ok_text]';
	if (lang.ok_text != undefined) {
		ok = lang.ok_text;
	}	
	
	var title = '[ask_jeweler_management]';
	if (lang.ask_jeweler_management != undefined) {
		title = lang.ask_jeweler_management;
	}	
	
	$("#question-details-modal").dialog({
		autoOpen: false,
		height: 330,
		width: 600,
		modal: true,
		title: title,
		buttons: [
		    {
				text: ok,
				click: function() {
					$(this).dialog("close");
				}
			}			
		]	
	});
	

	var ask = '[ask]';
	if (lang.ask != undefined) {
		ask = lang.ask;
	}
	
	var modal_title = '[ask_the_jeweler]';
	if (lang.ask_the_jeweler != undefined) {
		modal_title = lang.ask_the_jeweler;
	}
	
	var cancel = '[cancel]';
	if (lang.cancel != undefined) {
		cancel = lang.cancel;
	}
	
	$( "#ask_the_jeweler" ).dialog({
		autoOpen: false,
		height: 250,
		width: 400,
		modal: true,
		buttons: [
		          {
		        	  id: "button-ask",
		        	  text: ask,
		        	  click: function() {
		        		  if ($("#ask_the_jeweler").valid()){	
		        			  $.ajax({
		        				  url: settings.base_url + lang.lang_key + '/questions/save_question',
		        				  dataType: 'json',
		        				  type: 'POST',
		        				  data: {
		        					  form_data : $('#ask_the_jeweler').serialize()
		        				  },
		        				  success: function(data){
		        					  $("#question-container").hide();
		        					  $("#button-ask").hide();
		        					  if (data.status == "1") {
		        						  $( "#question-status-success" ).show();
		        						  return false;
		        					  } else {
		        						  $( "#question-status-error" ).show();
		        					  }
		        				  },
		        				  complete: function() {
		        				  }
		        			  });	
		        		  }									
		        	  }
		          },
		          {
		        	  text: cancel,
		        	  click: function() {
		        		  $(this).dialog('close');
		        	  }
		          }			
		          ],
		          close: function() {
		        	  $('#question').val('');
		          },
		          open: function(){
		        	  $("#question-container").show();
		        	  $(".message").hide();
		        	  $("#button-ask").show();
		          },
		          title : modal_title
	});
	
	var ok_text = '[ok_text]';
	if (lang.ok_text != undefined) {
		ok_text = lang.ok_text;
	}
}); 

function askQuestion(){
	$( "#ask_the_jeweler" ).dialog('open');
}

function showQuestion(id){
	$.ajax({
		 url: settings.base_url + lang.lang_key + '/questions/get_question/' + id,
	     dataType: 'json',
	     type: 'POST',
	     success: function(data){
	    	 if (data.status){
	    		 $("#question-details-modal").html(data.html);
	    		 $("#question-details-modal").dialog("open");
	          }
	     },
	})
}